using System;
using System.Collections.Generic;
using System.Text;

namespace pic4uApi
{
    class FormContentBuilder
    {
        public enum ContentType { TextPlain = 0, ImageJpeg = 1 };

        private StringBuilder m_stringBuilder = new StringBuilder();
        private String m_boundry;

        public FormContentBuilder(String boundary)
        {
            m_boundry = boundary;
        }

        public override string ToString()
        {
            return m_stringBuilder.ToString();
        }

        /*Content-Disposition: form-data; name="optPublic"
          Content-Type: text/plain; charset=utf-8; Content-Transfer-Encoding: 8bit

          True*/
        public void Add(ContentType contentType, String key, String value)
        {
            String contentTypeText = String.Empty;

            bool isFile = false;
            switch (contentType)
            {
                case ContentType.TextPlain:
                    contentTypeText = "text/plain; charset=utf-8; Content-Transfer-Encoding: 8bit";
                    break;
                case ContentType.ImageJpeg:
                    contentTypeText = "image/jpeg; charset=utf-8; Content-Transfer-Encoding: binary";
                    isFile = true;
                    break;
                default:
                    contentTypeText = "text/plain; charset=utf-8; Content-Transfer-Encoding: 8bit";
                    break;
            }

            m_stringBuilder.Append("--");
            m_stringBuilder.Append(m_boundry);
            m_stringBuilder.Append("\r\n");
            m_stringBuilder.Append("Content-Disposition: form-data; name=\"");
            m_stringBuilder.Append(key);

            if (true == isFile)
            {
                m_stringBuilder.Append("\"; filename=\"");
                m_stringBuilder.Append(value);
            }

            m_stringBuilder.Append("\"");
            m_stringBuilder.Append("\r\n");
            m_stringBuilder.Append("Content-Type: ");
            m_stringBuilder.Append(contentTypeText);
            m_stringBuilder.Append("\r\n");
            m_stringBuilder.Append("\r\n");

            if (false == isFile)
            {
                m_stringBuilder.Append(value);
                m_stringBuilder.Append("\r\n");
            }
        }
    }
}
